<?php
session_start();
require_once("conexion_trato/class.conexionDB.inc.php");

$select_sucursal = $_REQUEST['select_sucursal'];


$usu1 = $_SESSION['usu1'];
$contra1 = $_SESSION['contra1'];
//$select_sucursal = $_SESSION['valor'];
//echo $select_sucursal;
     

//validacion de usuarios

$conn = new conexionBD();
$conn->SeleccionBBDD('seguridad_valmar_tratos');
$sql= "select RutUsu, TipUsu ,NbUsu FROM JAG_CONTUSU where LogUsu = '$usu1' AND ContraUsu ='$contra1'";



$consulta1=$conn->EjecutarSQL($sql);

$row1=mssql_fetch_array($consulta1);

	$_SESSION['rut'] = $row1[0];
	$_SESSION['tip_usu'] = $row1[1];
	$_SESSION['nom_usu'] = $row1[2];
    $_SESSION['base'] = 'TRATO';

/*echo $_SESSION['rut']."<br>";
echo "$_SESSION[tip_usu]<br>";
echo "$_SESSION[nom_usu]<br>";
*/

if($row1[0] == "")
{
	header("Location: index.php");
	exit;
}


$sql2 = "SELECT PerSucCod FROM dbo.PERSONAL WHERE (dbo.PERSONAL.PerCod = '$row1[0]')"; 
$sql4 = "SELECT RemNomMes,RemAnoPro,RemMesPro FROM dbo.REMMES where RemEst = 'V'";
//echo "$sql2";

$conn2 = new conexionBD();
$conn2->SeleccionBBDD('db_erp');


$consulta4 = $conn2->EjecutarSQL($sql4);
$row4=mssql_fetch_array($consulta4);
$_SESSION['nombremes'] = $row4['RemNomMes'];
$_SESSION['anoproceso'] = $row4['RemAnoPro'];
$_SESSION['mesproceso'] = $row4['RemMesPro'];

$consulta3=$conn2->EjecutarSQL($sql2);

$row3=mssql_fetch_array($consulta3);
$_SESSION['sucursal'] = $row3[0];

//echo "$_SESSION[sucursal]";

if(!isset($_SESSION['sucursal']))
{
	header("Location: index.php");
	exit;
}

if($_SESSION['tip_usu'] == 2)
{
	if($_SESSION['sucursal'] != $select_sucursal)
	{
		//echo "entre";
		header("Location: index.php");
		exit;
	} 
}




if (!$row1[0])
{   
	header("Location: index.php");
	exit;
}
else
{
	$sql="select TipUsu,Bloqueo from JAG_ContUsu where RutUsu='$row1[0]'";
	//echo $sql;
	$conn = new conexionBD();
	$conn->SeleccionBBDD('seguridad_valmar_tratos');
	$consulta2=$conn->EjecutarSQL($sql);
	$row2=mssql_fetch_array($consulta2);
	
	//echo $row2[0]."<br>".$row2[1];
	if (($row2[0]==0) || ($row2[1]=='S'))
	{
		header("Location: index.php");
		exit;
	}
	
	
	else
	{
		header("Location: default.php");
		exit;
	}
						  
}

?>
